package com.ruoyi.project.mydy.movieinfo.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.mydy.movieinfo.domain.DlMovieInfo;
import com.ruoyi.project.mydy.movieinfo.service.IDlMovieInfoService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 电影信息Controller
 * 
 * @author mhg
 * @date 2021-10-10
 */
@Controller
@RequestMapping("/mydy/movieinfo")
public class DlMovieInfoController extends BaseController
{
    private String prefix = "mydy/movieinfo";

    @Autowired
    private IDlMovieInfoService dlMovieInfoService;

    @Value("${my_upload_path}")
    private String dirPath;

    @RequiresPermissions("mydy:movieinfo:view")
    @GetMapping()
    public String movieinfo()
    {
        return prefix + "/movieinfo";
    }

    /**
     * 查询电影信息列表
     */
    @RequiresPermissions("mydy:movieinfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DlMovieInfo dlMovieInfo)
    {
        startPage();
        List<DlMovieInfo> list = dlMovieInfoService.selectDlMovieInfoList(dlMovieInfo);
        return getDataTable(list);
    }

    /**
     * 导出电影信息列表
     */
    @RequiresPermissions("mydy:movieinfo:export")
    @Log(title = "电影信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DlMovieInfo dlMovieInfo)
    {
        List<DlMovieInfo> list = dlMovieInfoService.selectDlMovieInfoList(dlMovieInfo);
        ExcelUtil<DlMovieInfo> util = new ExcelUtil<DlMovieInfo>(DlMovieInfo.class);
        return util.exportExcel(list, "电影信息数据");
    }

    /**
     * 新增电影信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存电影信息
     */
    @RequiresPermissions("mydy:movieinfo:add")
    @Log(title = "电影信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(String name,Long kindId,Long regionId,Long yearsId,String language,Date screenDate,Long length,String alias,String produce,String plotIntroduction
    ,MultipartFile file,MultipartFile file1) throws IOException {
        DlMovieInfo dlMovieInfo = new DlMovieInfo();
        dlMovieInfo.setName(name);
        dlMovieInfo.setKindId(kindId);
        dlMovieInfo.setRegionId(regionId);
        dlMovieInfo.setYearsId(yearsId);
        dlMovieInfo.setLanguage(language);
        dlMovieInfo.setScreenDate(screenDate);
        dlMovieInfo.setLength(length);
        dlMovieInfo.setAlias(alias);
        dlMovieInfo.setProduce(produce);
        dlMovieInfo.setPlotIntroduction(plotIntroduction);

        //保存封面图片到相关路径
        //得到原始文件名
        String fileName = file.getOriginalFilename();
        //得到后缀名
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        //得到唯一文件名
        fileName = UUID.randomUUID()+suffix;
        //保存图片的路径
        File dirFile = new File(dirPath);
        //判断如果文件夹不存在 创建
        if (!dirFile.exists()){
            dirFile.mkdirs();
        }
        //得到文件的完整路径
        String filePath = dirPath+"/"+fileName;
        //保存文件  异常抛出
        file.transferTo(new File(filePath));
        //保存文件名
        dlMovieInfo.setImagePath(fileName);

        //保存电影原声曲到相关路径
        //得到原始文件名
        String fileName1 = file1.getOriginalFilename();
        //得到后缀名
        String suffix1 = fileName1.substring(fileName1.lastIndexOf("."));
        //得到唯一文件名
        fileName1 = UUID.randomUUID()+suffix1;
        //保存图片的路径
        File dirFile1 = new File(dirPath);
        //判断如果文件夹不存在 创建
        if (!dirFile1.exists()){
            dirFile1.mkdirs();
        }
        //得到文件的完整路径
        String filePath1 = dirPath+"/"+fileName1;
        //保存文件  异常抛出
        file1.transferTo(new File(filePath1));
        //保存文件名
        dlMovieInfo.setMovieSong(fileName1);
        return toAjax(dlMovieInfoService.insertDlMovieInfo(dlMovieInfo));
    }

    /**
     * 修改电影信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        DlMovieInfo dlMovieInfo = dlMovieInfoService.selectDlMovieInfoById(id);
        mmap.put("dlMovieInfo", dlMovieInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存电影信息
     */
    @RequiresPermissions("mydy:movieinfo:edit")
    @Log(title = "电影信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DlMovieInfo dlMovieInfo)
    {
        return toAjax(dlMovieInfoService.updateDlMovieInfo(dlMovieInfo));
    }

    /**
     * 删除电影信息
     */
    @RequiresPermissions("mydy:movieinfo:remove")
    @Log(title = "电影信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(dlMovieInfoService.deleteDlMovieInfoByIds(ids));
    }
}
